package com.zyk.scaffold.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyk.scaffold.common.utils.AssertUtil;
import com.zyk.scaffold.user.entity.TenantUserRole;
import com.zyk.scaffold.user.mapper.TenantUserRoleMapper;
import com.zyk.scaffold.user.service.TenantUserRoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 租户-用户-角色关联表 服务实现类
 * </p>
 *
 * @author zhouyk
 * @since 2021-12-06
 */
@Service
public class TenantUserRoleServiceImpl extends ServiceImpl<TenantUserRoleMapper, TenantUserRole> implements TenantUserRoleService {

    @Override
    public List<TenantUserRole> getUserRolesByUserId(Long userId) {
        QueryWrapper<TenantUserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.lambda()
                .eq(TenantUserRole::getTenantUserId, userId)
                .eq(TenantUserRole::getDeleted, Boolean.FALSE);
        return this.list(userRoleQueryWrapper);
    }
    @Override
    public TenantUserRole getUserRoleByRoleId(Long roleId) {
        QueryWrapper<TenantUserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper
                .lambda()
                .eq(TenantUserRole::getTenantRoleId, roleId)
                .eq(TenantUserRole::getDeleted, Boolean.FALSE);
        return this.getOne(userRoleQueryWrapper, Boolean.FALSE);
    }

    @Override
    public List<TenantUserRole> getUserRolesByUserIds(List<Long> userIds) {
        if(CollectionUtils.isEmpty(userIds)){
            return Collections.EMPTY_LIST;
        }
        QueryWrapper<TenantUserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper
                .lambda()
                .in(TenantUserRole::getTenantUserId, userIds)
                .eq(TenantUserRole::getDeleted, Boolean.FALSE);
        return this.list(userRoleQueryWrapper);
    }

    @Override
    public Boolean deletedUserRoleByUserId(Long userId, Long optionUserId) {
        UpdateWrapper<TenantUserRole> userRoleUpdateWrapper = new UpdateWrapper<>();
        userRoleUpdateWrapper
                .lambda()
                .eq(TenantUserRole::getTenantUserId, userId)
                .set(TenantUserRole::getUpdateUser, optionUserId)
                .set(TenantUserRole::getDeleted, Boolean.TRUE);
        return this.update(userRoleUpdateWrapper);
    }

    @Override
    @Transactional
    public Boolean UpdateBatch(List<TenantUserRole> lstTenantUserRole, Long userId, Long optionUserId) {
        if(CollectionUtils.isEmpty(lstTenantUserRole) || null == userId){
            return Boolean.TRUE;
        }
        deletedUserRoleByUserId(userId, optionUserId);
        return this.saveBatch(lstTenantUserRole);
    }

    @Override
    public List<TenantUserRole> getUserRoleByRoleIds(List<Long> roleIds) {
        if(CollectionUtils.isEmpty(roleIds)){
            return Collections.emptyList();
        }
        QueryWrapper<TenantUserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.lambda()
                .in(TenantUserRole::getTenantRoleId, roleIds)
                .eq(TenantUserRole::getDeleted, Boolean.FALSE);
        return this.list(userRoleQueryWrapper);
    }
}
